// pages/status/detail.js
var app = getApp()
Page({
  data:{
      statuses:[],
      comments:'',
      pageIndex:1,
      pageSize:10,
      status_id:0,
      user_id:app.user.SpaceUserId,
      comment_id:'',
      comment_text:''
  },
  onLoad:function(options){
    // 页面初始化 options为页面跳转所带来的参数
    this.setData({
      status_id:options.status_id,
    });
    var status_id = options.status_id;
    var that = this;
    //获取闪存信息
    wx.request({
      url: 'http://api.cnblogs.com/api/statuses/' + status_id,
      data: {
      },
      header: {
          "Content-Type":"application/x-www-form-urlencoded",
          "Authorization":"Bearer " + app.accessToken
      },
      success: function(res) {
        that.setData({
          statuses:res.data
        })
      }
    })
    this.loadStatusescomment(this, status_id, this.data.pageIndex, this.data.pageSize)
  },
  onReady:function(){
    // 页面渲染完成
  },
  onShow:function(){
    // 页面显示
  },
  onHide:function(){
    // 页面隐藏
  },
  onUnload:function(){
    // 页面关闭
  },
  formSubmit: function(e) {
    var content = e.detail.value.content;
    var status_id = e.detail.value.status_id;
    var comment_id = e.detail.value.comment_id;
    var that = this;
    //发表闪存评论
      wx.request({
          url: 'http://api.cnblogs.com/api/statuses/'+ status_id +'/comments',
          method: 'post',
          data: {
            ReplyTo:status_id,
            ParentCommentId:comment_id,
            Content:content
          },
        header: {
            "Content-Type":"application/x-www-form-urlencoded",
            "Authorization":"Bearer " + app.accessToken
        },
        success: function(res2) {
        },
        complete: function () {
            that.getStatus(that, status_id);
            that.onPullDownRefresh();
            that.setData({
              comment_text:''
            })
        }
      })
  },
  //下拉刷新
  onPullDownRefresh:function(){
    wx.showNavigationBarLoading();
    this.setData({
      pageIndex:1,
      statuses:[]
    });
    this.loadStatusescomment(this, this.data.status_id, this.data.pageIndex, this.data.pageSize)
  },
  //上拉加载更多
  onReachBottom:function(){
    this.setData({
      pageIndex:this.data.pageIndex + 1
    });
    this.loadStatusescomment(this, this.data.status_id, this.data.pageIndex, this.data.pageSize)
  },
  getStatus:function(that, status_id) {
      wx.request({
      url: 'http://api.cnblogs.com/api/statuses/' + status_id,
      data: {
      },
      header: {
          "Content-Type":"application/x-www-form-urlencoded",
          "Authorization":"Bearer " + app.accessToken
      },
      success: function(res) {
        that.setData({
          statuses:res.data
        })
      }
    })
  },
  //获取闪存评论
  loadStatusescomment:function(that, status_id, pageIndex, pageSize) {
      wx.request({
          url: 'http://api.cnblogs.com/api/statuses/'+ status_id +'/comments',
          data: {
          },
        header: {
            "Content-Type":"application/x-www-form-urlencoded",
            "Authorization":"Bearer " + app.accessToken
        },
        success: function(res2) {
          that.setData({
            comments:res2.data
          })
        },
        complete: function () {
            wx.hideNavigationBarLoading();
            wx.stopPullDownRefresh();
        }
      })
  },
  //删除评论
  deleteComment:function(event) {
    var that = this;
      wx.request({
          url: 'http://api.cnblogs.com/api/statuses/'+ event.target.dataset.status_id +'/comments/' + event.target.dataset.comment_id,
          method: 'delete',
          data: {
          },
        header: {
            "Content-Type":"application/x-www-form-urlencoded",
            "Authorization":"Bearer " + app.accessToken
        },
        success: function(res) {
          
        },
        complete: function () {
          that.getStatus(that, event.target.dataset.status_id);
            that.onPullDownRefresh();
        }
      })
  },
  //回复别人评论
  replyComment:function(event) {
    var comment_id = event.currentTarget.dataset.comment_id;
    var comment_user_name = event.currentTarget.dataset.comment_user_name;
    
    this.setData({
      comment_id:comment_id,
      comment_text:"@" + comment_user_name +"："
    });
  }
})